<?php
	ini_set('include_path', dirname(dirname(__FILE__)));
require_once(get_include_path().'/libraries/load.php');

function isFile($p){
	if($p[0]=="%" && $p[strlen($p)-1] == "%")
		return true;
	return false;
}
try{
	
	$str_method = $_POST['method'].'(';
	$params = json_decode(stripslashes($_POST['params']));
	
	$returnData = $_POST['returnData'];
	$start = (int)$_POST['start'];
	$limit = (int)$_POST['limit'];
	for( $i = 0; $i < count($params); $i++ ){
		if($i > 0)$comma = ',';
		switch(gettype($params[$i])){
			case 'string':
				if(isFile($params[$i]))
					$str_method .= $comma .'\'' . FileManager::SaveFile($_FILES[str_replace("%","",$params[$i])]) .'\'';
				else	
					$str_method .= $comma .'\'' .$params[$i] .'\'';
				break;				
			default:
				$str_method .= $comma . $params[$i];
				break;
		}
	}
	$str_method .= ');';


	if($returnData == "true"){
		$res = eval('return '.$str_method);
		$store = new storeResult();
		$store->totalCount = count($res);
		if( $start >= 0 && $limit > 0)
			$res = array_slice($res,$start, $limit);
		$store->values = $res;
		echo json_encode($store);
	}else{
		
		eval($str_method);
		echo '{success:true}';
	}
}catch(Exception $ex){
	echo '{success:false, method: \''. $str_method.'\', message: \''. $ex->getMessage().'\'}';
}

class storeResult{
	public $totalCount;
	public $values;
}
?>